package flexo.input;

public class Event implements Comparable<Event>
{
	protected boolean repeat;
	
	/**
	 * Name assigned to the event.
	 */
	protected String action;
	
	/**
	 * The event that generated the action
	 */
	protected int event;
	
	/**
	 * Value of the event. Usually in the range 0-1 except in mouse events.
	 * Useful only with analog events, but all events are treated as analog,
	 * so that the code is the same either way.
	 */
	protected float value;
	
	/**
	 * Which controller generated the event. Usually 0
	 * but if you have more than one gamepad it should be 0, 1, 2 and so on. 
	 */
	protected int id;
	
	public Event(String name, int event, boolean repeat)
	{
		this.action = name;
		this.event = event;
		this.repeat = repeat;
	}
	
	public Event(String name, int event)
	{
		this.action = name;
		this.event = event;
		this.repeat = false;
	}
	
	public boolean check(String actionName)
	{
		return action.equals(actionName);
	}
	
	public boolean repeat()
	{
		return repeat;
	}
	
	void setValue(float value)
	{
		if (value < 0) value = 0;
		//if (value > 1) value = 1;
		
		this.value = value;
	}
	
	void setID(int id)
	{
		if (id < 0) id = 0;
		this.id = id;
	}
	
	public String getName()
	{
		return action;
	}
	
	public int getEvent()
	{
		return event;
	}
	
	public float getValue()
	{
		return value;
	}
	
	public boolean isKeyboardEvent()
	{
		if (event >= 0 && event < EventType.KEY_COUNT) return true;
		return false;
	}
	
	public boolean isMouseEvent()
	{
		if (event >EventType.KEY_COUNT && event < EventType.MOUSE_COUNT) return true;
		return false;
	}
	
	public boolean isJoystickEvent()
	{
		if (event >EventType.MOUSE_COUNT && event < EventType.JOY_COUNT) return true;
		return false;
	}

	@Override
	public int compareTo(Event arg0)
	{
		if (arg0.action.equals(this.action) && event == arg0.event)
		{
			return 0;
		}
		else
		{
			if ( (event - arg0.event) == 0)
			{
				return 1;
			}
			else
			{
				return event - arg0.event;
			}
		}
	}
}
